<template>
  <div class="search-container">
    <mt-search v-model="value"
               cancel-text="取消"
               placeholder="搜索"
               autofocus>
    </mt-search>

    <div class="goods-list">
      <div class="goods-item"
           v-for="(item, index) in goodsList"
           :key="index"
           @click="goDetail(item.id)">
        <img :src="item.img_url">
        <h3 class="title">{{item.title}}</h3>
        <div class="info">
          <p class="price">
            <span class="new">￥{{item.new_price}}</span>
            <span class="old">￥{{item.old_price}}</span>
          </p>
          <p class="sell">
            <span>热卖中</span>
            <span>还剩{{item.stock_quantity}}</span>
          </p>
        </div>
      </div>

      <mt-button type="danger"
                 size="large"
                 @click="getMore"
                 v-if="goodsList.length">加载更多</mt-button>

    </div>

  </div>
</template>

<script>
import { Toast } from 'mint-ui';
export default {
  data() {
    return {
      value: '',
      page: 1,
      goodsList: []
    }
  },
  methods: {
    search() {
      this.getGoodsList();
    },
    getGoodsList() {
      if (this.value == '') return;
      this.axios.get(`/getgoods?title_like=${this.value}&_page=${this.page}&_limit=4`).then(res => {
        if (res.data.length == 0) {
          Toast('没有更多的商品了');
          return;
        }
        this.goodsList = res.data;
      })
    },
    getMore() {
      if (this.value == '') return;
      this.page++;
      this.axios.get(`/getgoods?title_like=${this.value}&_page=${this.page}&_limit=4`).then(res => {
        if (res.data.length == 0) {
          Toast('没有更多的商品了');
          return;
        }
        this.goodsList = this.goodsList.concat(res.data);
      })
    },
    goDetail(id) {
      this.$router.push({ name: 'goodsinfo', params: { id } });
    }
  },
  watch: {
    value(nVal) {
      this.page = 1;
      this.getGoodsList();
    }
  }
}
</script>

<style lang="scss" >
.search-container {
  .mint-search {
    height: 44px;
    .mint-searchbar-core {
      margin-bottom: 0;
      background-color: #fff;
      height: 28px;
    }
  }
  .mint-search-list {
    position: static;
  }
  .goods-list {
    display: flex;
    flex-wrap: wrap;
    padding: 8px;
    justify-content: space-between;
    .goods-item {
      width: 49%;
      border: 1px solid #ccc;
      box-shadow: 0 0 7px #eee;
      margin: 4px 0;
      padding: 2px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      min-height: 293px;
      h3 {
        line-height: 18px;
      }
      img {
        width: 100%;
        height: 180px;
      }
      .title {
        font-size: 14px;
      }
      .info {
        background-color: #eee;
        p {
          margin: 0;
          padding: 5px;
        }
        .price {
          .new {
            color: red;
            font-size: 16px;
            font-weight: 600;
          }
          .old {
            text-decoration: line-through;
            font-size: 12px;
            margin-left: 10px;
          }
        }
        .sell {
          display: flex;
          justify-content: space-between;
          font-size: 13px;
        }
      }
    }
  }
}
</style>